﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using WinnerCMS.Model.Attribute;
using WinnerCMS.PlugIn.OpenAPI.Config;

#endregion

namespace WinnerCMS.WebUI.Admin.ControlPanel
{
    [Access("265006")]
    public partial class API : WebFactory.PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindList();
            }
        }

        private void BindList()
        {
            this.GV_List.EmptyDataTemplate = GetTemplate(this);

            var config = APIConfigManage.GetConfig();
            this.cbkEnable.Checked = config.DPOEnable;
            this.txtMyID.Text = config.AppID;
            this.txtKey.Text = config.AppKey;
            this.txtDLLName.Text = config.DLLName;
            this.txtFullCategoryName.Text = config.FullCategoryName;

            this.GV_List.DataSource = config.NodeList;
            this.GV_List.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            var appInfo = new Node
            {
                AppId = this.txtID.Text,
                Url = this.txtURL.Text
            };

            var config = APIConfigManage.GetConfig() ?? new APIConfig();

            config.NodeList.Add(appInfo);

            APIConfigManage.SaveConfig(config);

            BindList();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var config = APIConfigManage.GetConfig();
            config.DPOEnable = this.cbkEnable.Checked;
            config.AppID = this.txtMyID.Text.Trim();
            config.AppKey = this.txtKey.Text.Trim();
            config.DLLName = this.txtDLLName.Text.Trim();
            config.FullCategoryName = this.txtFullCategoryName.Text.Trim();

            APIConfigManage.SaveConfig(config);

            MsgBox.Message("保存成功。", this.UpdatePanel1);
        }

        protected void GV_List_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var config = APIConfigManage.GetConfig() ?? new APIConfig();

            var index = -1;
            var id = this.GV_List.Rows[e.RowIndex].Cells[0].Text.ToLower();
            var url = ((TextBox) this.GV_List.Rows[e.RowIndex].Cells[1].Controls[1]).Text.ToLower();
            for (var i = 0; i < config.NodeList.Count; i++)
            {
                if (config.NodeList[i].AppId.ToLower() == id)
                {
                    index = i;
                }
            }
            if (index == -1)
            {
                return;
            }
            config.NodeList[index].Url = url;

            APIConfigManage.SaveConfig(config);

            this.GV_List.EditIndex = -1;
            BindList();
        }

        protected void GV_List_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.GV_List.EditIndex = e.NewEditIndex;
            BindList();
        }

        protected void GV_List_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.GV_List.EditIndex = -1;
            BindList();
        }

        protected void GV_List_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var config = APIConfigManage.GetConfig() ?? new APIConfig();

            var index = -1;
            var id = this.GV_List.Rows[e.RowIndex].Cells[0].Text.ToLower();

            for (var i = 0; i < config.NodeList.Count; i++)
            {
                if (config.NodeList[i].AppId.ToLower() == id)
                {
                    index = i;
                }
            }
            if (index == -1)
            {
                return;
            }
            config.NodeList.RemoveAt(index);

            APIConfigManage.SaveConfig(config);
            BindList();
        }
    }
}